#include "taskQueue.h"

//任务队列的初始化
int taskQueueInit(taskQueue_t* pqueue){
    //将队列置零
    bzero(pqueue,sizeof(taskQueue_t));
    return 0;
}
//尾插法插入新的任务
int taskAdd(taskQueue_t* pqueue,int netfd){
    node_t* newNode = (node_t*)calloc(1,sizeof(node_t));
    newNode->netfd = netfd;
    if(pqueue->queueSize == 0){
        pqueue->pRear = newNode;
        pqueue->pFront = newNode;
    }else{
        pqueue->pRear->pNext = newNode;
        pqueue->pRear = newNode;
    }
    (pqueue->queueSize)++;

    return 0;
}

//头删法删除要处理的任务
int taskDel(taskQueue_t* pqueue){
    node_t* temp = (node_t*)pqueue->pFront;
    pqueue->pFront = temp->pNext;
    //如果剩了最后一个任务节点
    if(pqueue->queueSize == 1){
        pqueue->pRear = NULL;
    }
    free(temp);
    (pqueue->queueSize)--;

    return 0;
}

//遍历任务队列中的所有任务
int taskQueuePrint(taskQueue_t* pqueue){
    node_t* temp = pqueue->pFront;
    while(temp != NULL){
        printf("%3d ",temp->netfd);
        //打印出当前任务的netfd之后,让temp临时节点指向下一个要打印的任务节点
        temp = temp->pNext;
    }
    printf("\n");
    return 0;
}
